package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.service.ReportMemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/report")
@Slf4j
public class ReportMemberController {

    @Reference(version = "1.0.0")
    private ReportMemberService reportMemberService;

    /**
     * 会员数量统计
     */
    @GetMapping("/member/count")
    public Result reportMemberCount(){
        List<String> months=new ArrayList<>();

        //获得当前时间
        Calendar calendar=Calendar.getInstance();
        //根据当前时间获取去年时间
        calendar.add(Calendar.YEAR,-1);
        //格式化时间 只需要年月
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");

        for (int i = 0; i <12; i++) {
            //月份每次+1  拿要从去年的月份+1开始拿
            calendar.add(Calendar.MONTH,1);
            Date time = calendar.getTime();
            log.info("拿到的月份为:{}",sdf.format(time));
            months.add(sdf.format(time));
        }

        log.info("拿到的所有月份为:{}",months);

        List<Integer> counts=new ArrayList<>();
        //拿到去年的所有的年月份 遍历
        for (String month : months) {
            //拿到每个月的注册的会员数 日期需要大于等于该月1号且小于等于该月31号
            Integer count = this.reportMemberService.queryMemberCount(month+"-1",month + "-31");
            log.info(month+"注册了"+count+"位用户");
            counts.add(count);
        }

        //返回前端集合
        Map<String,Object> map=new HashMap<>();
        map.put("months",months);
        map.put("memberCount",counts);
        Result result=new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);

        return result;
    }

}
